<?php

// 政策动态模型
class zhengcedongtaiModule extends Model
{
    public function __construct()
    {
        parent::__construct("zhengcedongtai");
    }

    /**
     * 查询政策动态列表
     * @date: 2019年6月13日 上午11:46:11
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function getzhengcekuList($startSet = 0, $endSet = 10, $orderBy = "", $whereStr = "status = 1")
    {
        if ($orderBy == "") {
            $orderBy = " fawenriqi desc,id desc";
        } else {
            $orderBy = $orderBy . " ,fawenriqi desc,id desc";
        }
        if ($whereStr == "") {
            $whereStr = " zhuangtai=2 ";
        } else {
            $whereStr = $whereStr . " and zhuangtai=2 ";
        }
        //自定义查询条件
        $searchdata = $this->getFieldArray();
        foreach ($searchdata as $k => $v) {
            if ($v != "") {
                if ($k == "fawenriqi") {
                    $temp_date = explode('|', $v); //$hello = explode(',',$source);
                    $date_start = trim($temp_date[0]);
                    $date_end = trim($temp_date[1]);

                    $whereStr = $whereStr . " and fawenriqi between '{$date_start}' and '{$date_end}' ";
                } else if ($k == "jibie") {
                    $whereStr = $whereStr . " and {$k} in ({$v}) ";

                } else {
                    $whereStr = $whereStr . " and {$k}='{$v}' ";
                }
            }
        }

        //全表模糊查询
        $search_value = $this->common->Requert("search_value");
        $sql = "";
        if ($search_value != "") {
            $sql = " (biaoti like '%{$search_value}%'  or neirong like '%{$search_value}%') ";
        }
        if ($sql != "") {
            $whereStr = $whereStr . " and " . $sql;
        }

        $list = $this
            ->find("id,biaoti,fawenriqi,renqi,fengmian,pailiefangshi")
            ->where($whereStr)
            ->orderBy($orderBy)
            ->limit(($startSet - 1) * $endSet, $endSet)
            ->all();

        //处理数据
        foreach ($list as $k => $v) {
            //处理标题
            $v["biaoti"] = DeleteHtml(html_entity_decode($v["biaoti"]));
            //处理发文日期
            if ($v['fawenriqi'] != "") {
                $v['fawenriqi'] = date("Y-m-d", strtotime($v['fawenriqi']));
            }
            if ($v['fengmian'] == "") {
                $v['pailiefangshi'] = '3';
            } else {
                //处理封面图 
                $v['fengmian'] = $this->common->getHostDomain() . $v['fengmian'];
            }
            $list[$k] = $v;
        }
        $re_list["data"] = $list;
        $re_list["count"] = count($this->where($whereStr)->all());
        return $re_list;
    }

    /**
     * 获取热搜词组
     * @date: 2019年6月13日 下午3:11:57
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function getHotWords($number = 3)
    {
        $list = $this
            ->find("biaoqian")
            ->where("biaoqian<>'' and biaoqian is not null and status = 1")
            ->orderBy("renqi desc")
            ->all();
        $words = "";
        foreach ($list as $k => $v) {
            if ($words == "") {
                $words = $v['biaoqian'];
            } else {
                $words = $words . "," . $v['biaoqian'];
            }
        }

        $wordsArray = $words == "" ? [] : explode(",", $words);
        $wordsArray = array_unique($wordsArray);
        $temp_wordsArray = [];
        foreach (array_rand($wordsArray, $number) as $k => $v) {
            $temp_wordsArray[] = $wordsArray[$v];
        }
        return $temp_wordsArray;

    }

    /**
     * 查询分类
     * @date: 2019年6月13日 下午3:56:16
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function getfenlei()
    {
        $data["jibie"][] = ["name" => "全部", "value" => ""];
        $data["fawenriqi"][] = ["name" => "全部", "value" => ""];

        $parameterM = new parameterModel();
        $sortM = new sortModel();
        $data["jibie"] = array_merge($data["jibie"], $parameterM->getparameterArrayByclassid("369"));

        //单独处理年份
        $cur_year = date('Y');
        for ($i = 0; $i < 3; $i++) {
            $temp_year = ($cur_year - $i);
            $jiduArray = [];
            $jidunameArray = ["一季度", "二季度", "三季度", "四季度"];
            for ($j = 1, $_j = 0; $j <= 12; $j += 3, $_j++) {
                $temp_mounth_start = $j;
                $temp_mounth_end = $j + 2;
                $jiduArray[] = ["name" => $jidunameArray[$_j], "value" => "{$temp_year}-{$temp_mounth_start}-1|{$temp_year}-{$temp_mounth_end}-31"];
            }
            $data["fawenriqi"][] = ["name" => $cur_year - $i, "value" => "{$temp_year}-1-1|{$temp_year}-12-31", "child" => $jiduArray];
        }

        //单独处理级别===>区
        foreach ($data["jibie"] as $k => $v) {
            if ($v["value"] == "4") {
                $v["child"][] = ["name" => "全部", "value" => $parameterM->getparameterChildIdsByclassid($v["id"], "u2")];
                $v["child"] = array_merge($v["child"], $parameterM->getparameterArrayByclassid($v["id"]));
                $v["value"] = $parameterM->getparameterChildIdsByclassid($v["id"], "u2");
            }
            $data["jibie"][$k] = $v;
        }
        return $data;
    }

    public function getzhengcedongtaiDetail($id)
    {
        $detail = $this->findOne($id);
        if (count($detail) > 0) {
            //增加人气
            $this->query("update sl_zhengcedongtai set renqi=renqi+1 where id={$id} ");
            //格式化日期
            $detail["fawenriqi"] = date("Y-m-d", strtotime($detail["fawenriqi"]));
            //替换内容图片和文件路径 neirong
            $detail["neirong"] = str_replace("/public/upload/", $this->common->getHostDomain() . "/public/upload/", $detail["neirong"]);
            $detail["neirong"] = str_replace("/upfile/Editor/", $this->common->getHostDomain() . "/upfile/Editor/", $detail["neirong"]);
            return $detail;
        } else {
            return [];
        }
    }

    /**
     * 查询相关列表
     * @date: 2019年6月16日 上午4:51:30
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function getrelationdongtai($id, $startSet = 0, $endSet = 10)
    {
        $list = $this
            ->find("id,biaoti,fawenriqi,renqi,fengmian")
            ->where("  id < {$id} and status = 1")
            ->limit(($startSet - 1) * $endSet, $endSet)
            ->orderby("jibie asc, id desc")
            ->all();

        //处理数据
        foreach ($list as $k => $v) {
            //处理标题
            $v["biaoti"] = DeleteHtml(html_entity_decode($v["biaoti"]));
            //处理发文日期
            if ($v['fawenriqi'] != "") {
                $v['fawenriqi'] = date("Y-m-d", strtotime($v['fawenriqi']));
            }
            if ($v['fengmian'] != "") {
                //处理封面图
                $v['fengmian'] = $this->common->getHostDomain() . $v['fengmian'];
            }
            $list[$k] = $v;
        }

        return $list;

    }

    /**
     * 单条政策数据格式
     * @date: 2019年6月16日 上午3:57:33
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function getonelistByid($id)
    {
        $data = $this->find("id,biaoti,description,biaoqian,fawenriqi")->findOne($id);
        if ($data) {
            //处理发文日期
            $v = $data;
            if ($v['fawenriqi'] != "") {
                $v['fawenriqi'] = date("Y-m-d", strtotime($v['fawenriqi']));
            }

            //处理标签
            $v['biaoqian'] = ($v['biaoqian'] == null || $v['biaoqian'] == "") ? [] : explode(",", $v['biaoqian']);

            //处理申请状态
            if ($v['jiezhiriqi'] == "" || $v['jiezhiriqi'] == null) {
                $v['zhuangtai'] = "";
            } else {
                $zero1 = time();
                $zero2 = strtotime($v['jiezhiriqi']);
                if ($zero1 < $zero2) {
                    //echo “zero1早于zero2′;
                    $v['zhuangtai'] = "申请中";
                } else {

                    $v['zhuangtai'] = "已过期";
                }
            }
            return $v;
        } else {
            return [];
        }
    }

    /**
     * 插入四方平台的数据
     * @date: 2019年9月2日 下午4:11:01
     * @param: variable
     * @return:
     * @author: 龚华尧
     */
    public function insertFromSifangdata($data)
    {
        if (!empty($data)) {
            //数据不存在则插入
            $_itemdata = $this->where("biaoti='{$data["biaoti"]}' ")->one();
            if (count($_itemdata) == 0) {
                //新增一条数据
                $_data["biaoti"] = $data["biaoti"];
                $_data["fengmian"] = $data["suoluetu"];
                $_data["laiyuan"] = $data["laiyuan"];
                $_data["neirong"] = $data["neirong"];
                $_data["renqi"] = $data["renqi"];
                $_data["paixu"] = $data["paixu"];
                $_data["biaoqian"] = $data["guanjianci"];
                $_data["fawenriqi"] = $data["dtime"];
                $_data["zhuangtai"] = 2;
                $_data["pailiefangshi"] = 3;
                $_data["dtime"] = date("Y-m-d H:i:s");

                //处理封面
                if ($_data["fengmian"] != "") {
                    //处理排列方式
                    $_data["pailiefangshi"] = 1;
                }
                //处理关键词
                $_data["biaoqian"] = str_replace(" ", ",", $_data["biaoqian"]);
                //设置默认值
                $_data["jibie"] = 3;//市
                $this->insert($_data);
            }
        }
    }

}